package com.shuqi.model;

import java.util.List;

import org.apache.http.message.BasicNameValuePair;

import android.content.Context;

import com.shuqi.common.MyTask;
import com.shuqi.common.Urls;
import com.shuqi.model.net.HttpRequest;
import com.shuqi.model.net.UrlManager;
import com.shuqi.model.net.listener.HttpRequestListener;
import com.shuqi.model.parse.parser.BookSearchParser;
import com.shuqi.model.parse.parser.BookSearchRankParser;
import com.shuqi.model.parse.parser.SearchAssociateParser;

/**
 * 搜索主页model
 * 
 * @author linyuren
 * 
 */
public class BookSearchModel extends ModelBase implements HttpRequestListener {

    private HttpRequest httpRequest;
    private Context context;
    /** 搜索URL传递类型 */
    private String type = "";
    /** 搜索排行url中拼接rankid */
    private String randId = "";

    public BookSearchModel(Context context) {
        this.context = context;
    }

    @Override
    public void action(int actionCode, Object object) {
        fireEvent(actionCode, object);
    }

    @Override
    public List<BasicNameValuePair> setParams() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public String getRequestUrl() {
        if (this.randId.equals("other")) {
            return Urls.getSearchAssociateURL(this.type); // 搜索联想
        } else {
            return Urls.getSearchUrl(this.type, this.randId);
        }
    }

    @Override
    public void onDestroy() {
        if (httpRequest != null) {
            httpRequest.abort();
        }
    }

    @Override
    public void loadData(Object... objs) {
        this.type = (String) objs[0];
        this.randId = (String) objs[1];
        this.httpRequest =
                new HttpRequest(context, UrlManager.COLUMN_DOMAIN_SEARCH, getRequestUrl(), null, this);
        if (this.randId.equals("")) {
            this.httpRequest.setDataParseAdapter(new BookSearchParser());
        } else if (this.randId.equals("other")) {
            this.httpRequest.setDataParseAdapter(new SearchAssociateParser());
        } else {
            this.httpRequest.setDataParseAdapter(new BookSearchRankParser());
        }
        MyTask.runInBackground(httpRequest, true);
    }

}
